import {Injectable} from '@nestjs/common';
// ORM
import {InjectRepository} from '@nestjs/typeorm';
import {Repository, UpdateResult} from 'typeorm';
import {UserEntity} from '../entity/user.entity';
import {UserDto} from '../dto/user.dto';

@Injectable()
export class UserService {
    constructor(
        @InjectRepository(UserEntity)
        private readonly userRepository: Repository<UserEntity>,
    ) {
    }

    async createOne(userDto: UserDto): Promise<UserEntity> {
        return await this.userRepository.save(userDto);
    }

    async editOneStatus({userId, status}): Promise<UpdateResult> {
        return await this.userRepository.update({userId}, {status});
    }
    async editOneErrorCount({userId, errorCount}): Promise<UpdateResult> {
        return await this.userRepository.update({userId}, {errorCount});
    }
    async getOneByUserId(userId: string): Promise<UserEntity> {
        const qb = this.userRepository.createQueryBuilder('user');
        qb.where({userId})
        return qb.getOne()
    }
}
